{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**通用最佳实践回顾**\n",
    "\n",
    "- 扩容会使迭代器/引用/指针失效，必要时 `reserve` 或使用 `std::pmr::vector` 控制分配。\n",
    "- 频繁 push 时优先 `emplace_back`，避免额外拷贝。\n",
    "- `erase` 会使该位置及之后迭代器失效，遍历删除时注意迭代器更新。\n",
    "- 对于 `bool` 专门化，若需引用语义请使用 `std::vector<char>` 或 `std::vector<std::uint8_t>`。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "| 序号 | 工程场景                | 文件名                       | 说明要点                                                     | 可运行示例代码       |\n",
    "| ---- | ----------------------- | ---------------------------- | ------------------------------------------------------------ | -------------------- |\n",
    "| 1    | 序列化 / 网络包缓冲     | `serialization_buffer.cpp`   | `std::vector<std::uint8_t>` 作为可增长字节缓冲，`reserve` 减少扩容 | 示例 1：序列化缓冲   |\n",
    "| 2    | 科学计算 / 图形顶点数据 | `graphics_vertex_buffer.cpp` | 连续浮点数据，用 `data()` 直通渲染 / 计算管线                | 示例 2：图形顶点缓冲 |\n",
    "| 3    | 搜索 / 数据库结果集缓存 | `search_results_cache.cpp`   | `erase-remove` 清理无效项，分页返回                          | 示例 3：检索结果缓存 |\n",
    "| 4    | 游戏 ECS 组件池         | `ecs_component_pool.cpp`     | AoS/SoA 连续存储，`emplace_back` 构造组件                    | 示例 4：ECS 组件池   |\n",
    "| 5    | GPU/加速器数据传输      | `gpu_memory_transfer.cpp`    | `data()` 与 GPU API 交互，示例内置 CUDA stub 保障可运行      | 示例 5：GPU 内存传输 |\n",
    "| 6    | 日志批量写出            | `batched_logger.cpp`         | 批量追加、`clear` 重用容量、落盘                             | 示例 6：日志批量写出 |\n",
    "| 7    | 排序查询表              | `sorted_lookup_table.cpp`    | 构建期排序，运行期 `lower_bound` 查询                        | 示例 7：排序查询表   |"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "vscode": {
     "languageId": "shellscript"
    }
   },
   "outputs": [],
   "source": [
    "! rm -fr build"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 示例 1：序列化缓冲 (serialization_buffer.cpp)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "vscode": {
     "languageId": "shellscript"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Packet size: 13 bytes\n",
      "0x34 0x12 0xcd 0xab 0x01 0x00 0x05 0x00 \n",
      "0x48 0x65 0x6c 0x6c 0x6f \n"
     ]
    }
   ],
   "source": [
    "! g++ -std=c++17 -O2 serialization_buffer.cpp -o build/serialization_buffer && ./build/serialization_buffer"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 示例 2：图形顶点缓冲 (graphics_vertex_buffer.cpp)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "vscode": {
     "languageId": "shellscript"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Generated vertices: 153\n",
      "First vertex position: (0, 1, 0)\n"
     ]
    }
   ],
   "source": [
    "! g++ -std=c++17 -O2 graphics_vertex_buffer.cpp -o build/graphics_vertex_buffer && ./build/graphics_vertex_buffer"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 示例 3：检索结果缓存 (search_results_cache.cpp)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "vscode": {
     "languageId": "shellscript"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Remaining results: 2\n",
      "docA score=0.95\n",
      "docC score=0.87\n"
     ]
    }
   ],
   "source": [
    "! g++ -std=c++17 -O2 search_results_cache.cpp -o build/search_results_cache && ./build/search_results_cache"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 示例 4：ECS 组件池 (ecs_component_pool.cpp)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "vscode": {
     "languageId": "shellscript"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Entity 0 position: (0.16, 0.08, 0.032)\n",
      "Entity 1 position: (-1.952, 1.016, 0)\n"
     ]
    }
   ],
   "source": [
    "! g++ -std=c++17 -O2 ecs_component_pool.cpp -o build/ecs_component_pool && ./build/ecs_component_pool"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 示例 5：GPU 内存传输 (gpu_memory_transfer.cpp)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "vscode": {
     "languageId": "shellscript"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Upload succeeded. Device memory address: 0x5c47a2e1bf00\n"
     ]
    }
   ],
   "source": [
    "! g++ -std=c++17 -O2 gpu_memory_transfer.cpp -o build/gpu_memory_transfer && ./build/gpu_memory_transfer"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 示例 6：日志批量写出 (batched_logger.cpp)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "vscode": {
     "languageId": "shellscript"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Logs written to logs.txt\n"
     ]
    }
   ],
   "source": [
    "! g++ -std=c++17 -O2 batched_logger.cpp -o build/batched_logger && ./build/batched_logger"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 示例 7：排序查询表 (sorted_lookup_table.cpp)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "vscode": {
     "languageId": "shellscript"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Found alice -> 1001\n",
      "No record for dave\n"
     ]
    }
   ],
   "source": [
    "! g++ -std=c++17 -O2 sorted_lookup_table.cpp -o build/sorted_lookup_table && ./build/sorted_lookup_table"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "C++17",
   "language": "C++17",
   "name": "xcpp17"
  },
  "language_info": {
   "codemirror_mode": "text/x-c++src",
   "file_extension": ".cpp",
   "mimetype": "text/x-c++src",
   "name": "c++",
   "version": "17"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
